package com.xw.comm.service.impl;

import com.xw.comm.common.Constants;
import com.xw.comm.entity.ActionTypeEnum;
import com.xw.comm.entity.Common;
import com.xw.comm.entity.IdentifyObject;
import com.xw.comm.service.ImageActionService;
import com.xw.comm.service.LogService;
import com.xw.comm.utils.ObjectQueueUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xiongwei_vendor
 * @date 2021/4/26 19:22
 */
@Slf4j
@Service
public class ImageActionServiceImpl implements ImageActionService {

    @Autowired
    LogService logService;


    @Override
    public void action(String value, ActionTypeEnum actionTypeEnum) {
        switch (actionTypeEnum) {
            case YOLO3:
                yolo3Action(value);
                break;
            case YOLO5:
                yolo5Action(value);
                break;
            case MASTER:
                masterAction(value);
                break;
            default:
                break;
        }
    }

    private void yolo3Action(String value) {
    }


    /**
     * YOLO5 动作
     * pyth文件的数据是，x,y,w,h
     * x，y 是坐标
     * w、h是宽和高
     *
     * @param value 中心坐标，宽、高的值都要除以原图的宽、高
     */
    private void yolo5Action(String value) {
        if (StringUtils.isEmpty(value)) {
            return;
        }
        String[] item = value.trim().split(" ");
        if (item.length != Constants.IMAGE_LOCATION_YOLO) {
            log.info("接收的参数 = {} 出现问题，长度不够{}", value, Constants.IMAGE_LOCATION_YOLO);
            return;
        }

        IdentifyObject coordinateObject = new IdentifyObject();
        coordinateObject.setIdentifyObject(Integer.parseInt(item[0]));
        coordinateObject.setTopX(Double.parseDouble(item[1]));
        coordinateObject.setTopY(Double.parseDouble(item[2]));
        coordinateObject.setWidth(Double.parseDouble(item[3]));
        coordinateObject.setHeight(Double.parseDouble(item[4]));
        boolean addData = ObjectQueueUtil.identifyQueue.offer(coordinateObject);
        if (!addData && logService.isTruePrint("ImageActionServiceImpl.yolo5Action")) {
            log.info("add queue fail , value = {}", value);
        }

    }


    /**
     * fastMaster 动作
     *
     * @param value 值
     */
    private void masterAction(String value) {

    }


    public static void main(String[] arg0) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        ObjectQueueUtil.identifyQueue.take();
                    } catch (InterruptedException e) {
                    }
                    log.info("size = {}", ObjectQueueUtil.identifyQueue.size());
                    if (ObjectQueueUtil.identifyQueue.size() == 0) {
                        break;
                    }
                }
            }
        }).start();

        for (int i = 0; i < Common.OBJECT_QUEUE_COUNT + 5; i++) {
            log.info("i = {}", i);
            IdentifyObject coordinateObject = new IdentifyObject();
            boolean isOffer = false;
            try {
                ObjectQueueUtil.identifyQueue.put(coordinateObject);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            log.info("put ok = {}", isOffer);
        }

    }


}
